<script>
import store from "./store/index.js"
import scanGoStation from "./util/scanGoStation.js"
import settings from "./settings.js"
import {mapMutations} from "vuex";
  let updateManager = null
  let canIUse = true

  class WxUpdate {
    updateManager = wx.getUpdateManager();
    canIUse = wx.canIUse('getUpdateManager');

    static getInstance() {
      if (!this.instance) {
        this.instance = new WxUpdate();
      }
      return this.instance
    }

    check() {
      const that = this
      if (that.canIUse) {
        that.updateManager.onCheckForUpdate(function(res) {
          if (res.hasUpdate) {
            wx.showModal({
              title: '升级提示',
              content: '检测到新版本，是否下载新版本并重启小程序？',
              showCancel: false,
              success: function(res) {
                if (res.confirm) {
                  that.downLoadAndUpdateApplet()
                } else if (res.cancel) {
                  wx.showModal({
                    title: '温馨提示~',
                    content: '本次版本更新比较重要，旧版本可能无法正常访问的哦~',
                    showCancel: false,
                    confirmText: "确定更新",
                    success: function(res) {
                      if (res.confirm) {
                        that.downLoadAndUpdateApplet()
                      }
                    }
                  })
                }
              }
            })
          }
          // 不走if就是最新版
        })
      } else {
        that.downLoadAndUpdateWeChat()
      }
    }
    downLoadAndUpdateApplet() {
      const that = this;
      wx.showModal({
        title: '升级提示',
        showCancel: false,
        content: '如果小程序自动升级失败~请您手动删除当前小程序后，重新打开小程序~',
        success: function(res) {
          if(settings.upWithReset){
            store.commit("resetApplet")
          }
          wx.showLoading();
          that.updateManager.onUpdateReady(function() {
            wx.hideLoading();
            that.updateManager.applyUpdate()
          })
        }
      })

      that.updateManager.onUpdateFailed(function() {
        wx.hideLoading();
        wx.showModal({
          title: '已经有新版本了哟~',
          content: '新版本已经上线啦~，请您删除当前小程序，重新从公众号或搜索打开小程序哟~',
        })
      })
    }
    downLoadAndUpdateWeChat() {
      wx.showModal({
        title: '重要提示',
        content: '您当前微信版本过低，使用过程可能会不稳定。点击确认立刻升级微信。',
        success: function(res) {
          if (res.confirm) {
            wx.updateWeChatApp({
              success: function(res) {
                console.log('res', res)
              },
              fail: function(err) {
                console.log('err', err)
              },
            })
          }
        }
      })
    }
  }

  export default {
    onLaunch: async function(e) {
      // #ifdef  MP-ALIPAY
      // if (e.scene != "1011" || e.path != "pages/skip/skip") {
      //   return
      // }
      let res = ''
      if (e.query && e.query.qrCode) {
        res = e.query.qrCode;
      }
      await scanGoStation(res)
      // #endif

      //#ifdef MP-WEIXIN
      WxUpdate.getInstance()
      //#endif
    },
    methods: {
      ...mapMutations({
        updateUserInfo: "updateUserInfo",
      }),
    },
    onShow: function() {
      //#ifdef MP-WEIXIN
      const wxUpdate = WxUpdate.getInstance()
      wxUpdate.check();
      const _this = this;
      //#endif
      wx.getUserInfo({
        success: function (res) {
          const userInfo = {
            nickName: res.userInfo.nickName,
            avatarUrl: res.userInfo.avatarUrl,
            gender: res.userInfo.gender,
          }
          _this.updateUserInfo(userInfo);
        }
      })
    },
    onHide: function() {
      console.log('App Hide')
    }
  }
</script>

<style>
  /*每个页面公共css */
</style>
